package com.xuxin.pda.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.xuxin.pda.bean.BaseBean;
import butterknife.ButterKnife;

/**
 * Created by xu_xin
 * Created on 2020.06.05
 */
public abstract class BaseFragment extends Fragment implements View.OnClickListener {
    public Context context;
    public LoadingPopupView popup;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        context = getContext();
        View view = setContentView(inflater, container);
        ButterKnife.bind(this, view);
        initView();
        initPopup();
        initData();
        return view;
    }

    /**
     * 设置布局
     */
    public abstract View setContentView(@NonNull LayoutInflater inflater,
                                        @Nullable ViewGroup container);

    /**
     * 初始化布局
     */
    public abstract void initView();

    /**
     * 初始化数据
     */
    public abstract void initData();

    /**
     * 网络请求完成
     */
    public abstract void onComplete();

    public void initPopup() {
        popup = new XPopup.Builder(context)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .asLoading("正在保存，请稍后...");
    }

    public void dismiss() {
        if (null != popup) {
            popup.smartDismiss();
        }
    }

    public void onError(BaseBean baseBean) {
        if (null != baseBean) {
            ToastUtils.showShort(baseBean.getError().getMessage());
        } else {
            ToastUtils.showShort("网络连接异常");
        }
        onComplete();
    }
}